// miniprogram/pages/login/index.js
const App = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // phone: '',
        // password: '',
        // errmsg: '',
        // errmsg2: '',
        isLogin: wx.getStorageSync('token') || null,
        gradientColor: {
            '0%': '#F0F4FF',
            '88%': '#487df1',
            '100%': '#3461D8',
        },
    },

    pagesAddr: '/pages/mine/index',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let {
            screenWidth,
            screenHeight
        } = wx.getSystemInfoSync();
        this.setData({
            screenWidthMax: screenWidth,
            screenWidth: screenWidth / 2 - 62,
            screenHeight: screenHeight / 2 - 184
        });
        //设置跳转页面
        if(options.pagesAddr){
          this.pagesAddr = options.pagesAddr
        }
        
        let isLogin = wx.getStorageSync('token');
        this.setData({
            isLogin
        })
        if (isLogin) {
          //获取用户基本信息并跳到登录页面
        this.getUser();
        }
    },
    async onLogin(e) {
        //if (e.detail.code) {
            wx.login({
                success: async (reslogin) => {
                    // let DataBack = await App.$http("/login", { oidcode: reslogin.code, phonecode: e.detail.code, promotioncode: wx.getStorageSync('promotioncode') });
                    let DataBack = await App.$http("/app/user/xcxLogin?xcxCode=" + reslogin.code);
                    console.log(DataBack)
                    //登录成功，去获取用户数据
                    let userData = await App.$http("/app/user/getInfo",{},"GET");
                    if (userData) {
                        wx.setStorageSync('user', userData.data.user);
                        wx.setStorageSync('firstLogin', 1);
                        this.toHome();
                    } else {
                        wx.showModal({
                            title: '提示',
                            showCancel: false,
                            confirmText: "我知道了",
                            content: '此用户名暂无权限，请联系开发人员'
                        });
                    }
                },
            })
        //}
        return
        let {
            phone,
            password
        } = this.data;
        if (!(/^1\d{10}$/.test(phone))) {
            this.setData({
                errmsg: "手机号码有误"
            })
            return false;
        }
        if (password.length < 6 || password.length > 20) {
            this.setData({
                errmsg2: "密码长度为6~20位"
            })
            return false;
        }
        this.setData({
            errmsg: "",
            errmsg2: ""
        });
        try {
            let DataBack = await App.$http("/app/user/login", {
                phone,
                password
            });
            //登录成功，去获取用户数据
            let userData = await App.$http("/app/user/getInfo",{},"GET");
            if (userData) {
                wx.setStorageSync('user', userData.user);
                this.toHome();
            } else {
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    confirmText: "我知道了",
                    content: '此用户名暂无权限，请联系开发人员'
                });
            }
        } catch (error) {
            App.showModal(error)
        }

    },
    toHome() {
        wx.switchTab({
            url: this.pagesAddr
        });
    },
    onSetPhone(e) {
        this.setData({
            phone: e.detail
        })
    },
    onSetPassword(e) {
        this.setData({
            password: e.detail
        })
    },

    async getUser(){
      let userData = await App.$http("/app/user/getInfo",{},"GET");
      if (userData) {
        console.log(userData)
        wx.setStorageSync('user', userData.data.user);
        wx.setStorageSync('firstLogin', 1);
        this.toHome();
    }
    },

});